package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.xgkj.diyiketang.util.ToastUtil;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.VideoAdapter;
import com.xgkj.diyiketang.data.VideoInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.AnchorListTask;
import com.xgkj.diyiketang.task.GetOtherVideoListTask;
import com.xgkj.diyiketang.util.EmptyDataUtil;

import java.util.ArrayList;
import java.util.List;

public class MyVideoListActivity extends BaseActivity {

    private Context mContext;
    private PullToRefreshGridView mListView;
    private VideoAdapter mAdapter;
    private int mMyVideoTaskId = -1;
    private int size = 10;
    private int currentPage = 1;
    private LinearLayout mEmptyView;
    private TextView back;

    private LinearLayout mEmptyContentView;
    private String mUserId;
    private TextView mTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myvideolist);
        mContext = this;
        initView();
        getVideoList();
    }

    private void getVideoList(){
        if(currentPage == 0){
            mListView.onRefreshComplete();
            mListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
//            ToastUtil.showMessage(mContext, R.string.no_more);
            return;
        }
        if(mMyVideoTaskId != -1){
            return;
        }
        GetOtherVideoListTask myVideoListTask = new GetOtherVideoListTask(size,currentPage,mUserId);
        myVideoListTask.addListener(myVideoListTaskListener);
        mMyVideoTaskId = getEatShowApp().getTaskManager().addTask(myVideoListTask);

    }

    private TaskEventListener myVideoListTaskListener = new ShowToastTaskEventListener(this){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if(taskId == mMyVideoTaskId){
                mMyVideoTaskId = -1;
                mListView.onRefreshComplete();
                if (currentPage == 1){
                    mAdapter.clearData();
                }
                List<VideoInfo> videoList = new ArrayList<VideoInfo>();
                if(data != null){
                    videoList = (List<VideoInfo>) data;
                }

                if(videoList != null && videoList.size()>0){
                    mAdapter.updateData(videoList);
                }

                if(currentPage ==1 && mAdapter.getCount()>0){
                    mListView.setMode(PullToRefreshBase.Mode.BOTH);
                }

                currentPage = result;
                if(mAdapter != null && mAdapter.getCount() == 0){
                    EmptyDataUtil.updateEmptyContentViewGrid(mEmptyView, mEmptyContentView, mListView);
                }else{
                    EmptyDataUtil.updateNoEmptyContentViewGrid(mEmptyView, mEmptyContentView, mListView);
                }
            }

        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if(taskId == mMyVideoTaskId){
                mMyVideoTaskId = -1;
                mListView.onRefreshComplete();
            }
        }
    };

    private void initView() {
        mTitle = (TextView) findViewById(R.id.title);
        Intent intent = getIntent();
        if (intent != null) {
            mUserId = intent.getStringExtra("USERID");
        }

        if (mUserId == null){
            mTitle.setText("我的上传");
            mUserId = getEatShowApp().getmUserInfo().getUserId();
        }else {
            mTitle.setText("ta的课程");
        }

        mEmptyView = (LinearLayout) findViewById(R.id.content_error_empty);
        mEmptyView.setVisibility(View.GONE);
        mEmptyContentView = (LinearLayout) findViewById(R.id.empty_content_view);
        mEmptyContentView.setVisibility(View.GONE);
        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mListView = (PullToRefreshGridView) findViewById(R.id.myvideo_list);
        mAdapter = new VideoAdapter(mContext);
        mListView.setAdapter(mAdapter);
        mListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
                currentPage = 1;
                getVideoList();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
                getVideoList();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();

    }
}
